﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using phudieuhungthinh.BL;

namespace phudieuhungthinh.Admin
{
    public partial class HomeUploaded : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                pnlCategory.Controls.Add(new LiteralControl(LayoutProducts()));

                //string menuHtml = HelperCache.Get<string>(MenuKey);
                //if (menuHtml == null)
                //    lock (this)
                //    {
                //        if (menuHtml == null)
                //        {
                //            menuHtml = BuildMenu();
                //            HelperCache.Add(MenuKey, menuHtml, HelperCache.CachingTime.ShortTermSliding);
                //        }
                //    }

                //pnlMenuContainer.Controls.Add(new LiteralControl(menuHtml));
            }
        }

        //private string BuildMenu()
        //{
        //    StringBuilder menuBuilder = new StringBuilder("<div class='end'>");
        //    CategoryImpl im = new CategoryImpl();
        //    var menus = im.GetCategoryMenus();

        //    menuBuilder.Append("<ul>");
        //    foreach (var item in menus)
        //    {
        //        menuBuilder.AppendFormat("<li><a href='/Admin/ProductDirectory.aspx?cateid={1}'>{0}</a>", item.Name, item.ID);
        //        if (item.SubMenus.Count > 0)
        //        {
        //            menuBuilder.Append("<ul>");
        //            foreach (var subItem in item.SubMenus)
        //            {
        //                menuBuilder.AppendFormat("<li><a href='/Admin/ProductDirectory.aspx?cateid={1}'>{0}</a></li>", subItem.Name, subItem.ID);
        //            }
        //            menuBuilder.Append("</ul>");
        //        }
        //        menuBuilder.Append("</li>");
        //    }
        //    menuBuilder.Append("</ul>");
        //    menuBuilder.Append("</div>");

        //    return menuBuilder.ToString();
        //}

        protected string LayoutProducts()
        {
            var html = new StringBuilder();
            var im = new ProductImpl();
            List<string> items = im.GetHighlightProducts(Server.MapPath("~/Images/highlight-products"));

            html.Append("<div id='product-list'>");
            html.Append("<div id='list'>");
            html.Append("<ul class='full'>");

            for (int i = 0; i < items.Count; i++)
            {
                html.Append("<li class='item'>");
                html.Append("<div class='img'>");
                if (items[i] != null)
                {
                    html.AppendFormat("<img src='/Images/highlight-products/{0}' alt='{0}' />", items[i]);
                }
                html.Append("</div>");
                html.AppendFormat(
                    "<div class='roundedOne'><input type='checkbox' value='None' id='rounded{0}' name='check' /><label for='rounded{0}'></label></div>",
                    i);
                html.AppendFormat("<div class='title'><span>{0}</span></div>", items[i]);
                html.Append("</li>");
            }
            html.Append("</ul>");
            html.Append("</div>");
            html.Append("</div>");

            html.Append("<div class='button remove'><a href='javascript:void(0)'>Gỡ bỏ</a></div>");
            html.Append("<div class='button upload'><a href='javascript:void(0)'>Up hình</a></div>");

            return html.ToString();
        }

        [WebMethod(EnableSession = true)]
        public static bool DeleteFiles(string files)
        {
            try
            {
                string path = HttpContext.Current.Server.MapPath("~/Images/highlight-products");
                string[] aFiles = files.Split(';');

                foreach (string item in aFiles)
                {
                    if (item == string.Empty) continue;

                    string filePath = Path.Combine(path, item);
                    if (File.Exists(filePath)) File.Delete(filePath);
                }

                return true;
            }
            catch (Exception)
            {
                throw;
            }
        }

        [WebMethod(EnableSession = true)]
        public static bool MoveFiles(string files)
        {
            try
            {
                string sourceDir = HttpContext.Current.Server.MapPath("~/Images/products/copyright");
                string destinationDir = HttpContext.Current.Server.MapPath("~/Images/highlight-products");
                string[] aFiles = files.Split(';');

                foreach (string item in aFiles)
                {
                    if (item == string.Empty) continue;

                    string filePath = Path.Combine(sourceDir, item);
                    string desPath = Path.Combine(destinationDir, item);
                    if (File.Exists(filePath) && !File.Exists(desPath)) File.Move(filePath, desPath);
                }

                return true;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}